package com.smcdd.cdb.dbo.cdb_dbo.ui.adapter;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;
import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Page;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Tain;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Tain_v2;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DensityUtil;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.DrawableTextView;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.GlideRoundTransform;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.SimpleDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Administrator on 2017/6/20 14:20
 * 邮箱：zhanghuaiha@gmail.com
 */

public class TainAdapter extends RecyclerView.Adapter<TainAdapter.ViewHolder> {
    private Context mContext;
    private OnItemClickListenter onItemClickListenter;
    private OnItemsClickListenter onItemsClickListenter;
    private List<Tain_v2> mList;
    private SimpleDialog selfDialog;
    private boolean flag = true;
    private boolean isDay = true;
    private int stay = 0;
    private int success = 0;

    public TainAdapter(Context context){
        mContext = context;
        mList = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_yet_shop,parent,false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Tain_v2 tain = mList.get(position);
        Tain_v2.StoreInfoBean store_info = mList.get(position).getStore_info();



        if (store_info.getStore_type().equals("4")) {
//            holder.name.setDrawable(mContext.getResources().getDrawable(R.drawable.mimasuo));
//            if (tain.getMima_already_num() != null) {
//                holder.yp.setText("："+tain.getMima_already_num());
//            }else {
//                holder.yp.setText("：0");
//            }

            if (tain.getStatus().equals("1")) {
                holder.lxl.setText("：维护成功");
                holder.page_jin.setText("当日设备详情");
            }else {
                holder.lxl.setText("：待维护");
                holder.page_jin.setText("昨日设备详情");
            }

        }else  {
//            if (tain.getAlready_num() != null) {
//                holder.yp.setText("："+tain.getAlready_num());
//            }else {
//                holder.yp.setText("：0");
//            }
//            if (store_info.getStore_type().equals("1")){
//                holder.name.setDrawable(mContext.getResources().getDrawable(R.drawable.wifi));
//            }else {
//                holder.name.setDrawable(mContext.getResources().getDrawable(R.drawable.wu));
//            }

            if (flag) {
                if (tain.getStatus().equals("1")) {
                    holder.lxl.setText("：维护成功");
                    holder.page_jin.setText("当日设备详情");
                }else {
                    holder.lxl.setText("：待维护");
                    holder.page_jin.setText("昨日设备详情");
                }
            }else {
                holder.lxl.setText("：已维护");
                holder.page_jin.setText("当日设备详情");
            }
        }
        if (tain.getAlready_num().equals("0") && tain.getMima_already_num().equals("0")) {
            holder.name.setDrawable(mContext.getResources().getDrawable(R.drawable.wu));
            holder.yp.setText("：0");
        }else if (tain.getMima_already_num().equals("0")) {
            holder.name.setDrawable(mContext.getResources().getDrawable(R.drawable.wifi));
            holder.yp.setText("："+tain.getAlready_num());
        }else if (tain.getAlready_num().equals("0")) {
            holder.name.setDrawable(mContext.getResources().getDrawable(R.drawable.mimasuo));
            holder.yp.setText("："+tain.getMima_already_num());
        }else {
            holder.name.setDrawable(mContext.getResources().getDrawable(R.drawable.wu));
            holder.yp.setText("：0");
        }


        holder.name.setText(store_info.getName());
        holder.user.setText(store_info.getContact());
        holder.tel.setText(store_info.getTel());

        RequestOptions myOptions = new RequestOptions()
                .transform(new GlideRoundTransform(mContext,10));

        Glide.with(mContext)
                .asBitmap()
                .load(store_info.getImg_url())
                .apply(myOptions)
                .into(holder.icon);


        if (isDay) {
            holder.page_today.setVisibility(View.GONE);
        }else{
            if (tain.getStatus().equals("1")) {
                holder.page_today.setVisibility(View.GONE);
            }else {
                holder.page_today.setVisibility(View.VISIBLE);
            }
        }

        //设置地址
//        holder.location.setText(store_info.getProvince_name()+store_info.getCity_name()+store_info.getArea_name()+store_info.getSpecific_address());
        holder.location.setText(store_info.getArea_name()+store_info.getSpecific_address());
        holder.page_jin.setOnClickListener(v -> {
            onItemClickListenter.itemClickListenter(mList.get(position),position,flag,1);
        });

        holder.page_today.setOnClickListener(v -> {
            onItemClickListenter.itemClickListenter(mList.get(position),position,flag,2);
        });

        holder.tel.setOnClickListener(v -> {
            Dialog(position);

        });
        holder.page_rl.setOnClickListener(v -> {
            onItemsClickListenter.itemsClickListenter(mList.get(position),position);
        });
    }

    //拨打电话
    private void Dialog(int posotion) {
        selfDialog = new SimpleDialog(mContext);
        selfDialog.setTitle("提示");
        selfDialog.setMessage("是否拨打该号码："+mList.get(posotion).getStore_info().getTel());
        selfDialog.setYesOnclickListener("呼叫", () -> {
            //用intent启动拨打电话
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mList.get(posotion).getStore_info().getTel()));
            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            mContext.startActivity(intent);
            selfDialog.dismiss();
        });
        selfDialog.setNoOnclickListener("取消", () -> {
            selfDialog.dismiss();
        });
        selfDialog.show();
    }



    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        TextView user,tel,location,id;
        DrawableTextView yp,lxl,page_jin,name,page_today;
        ImageView icon;
        RelativeLayout page_rl;
        public ViewHolder(View itemView) {
            super(itemView);
            icon = (ImageView) itemView.findViewById(R.id.page_info_icon);
            name = (DrawableTextView) itemView.findViewById(R.id.page_name);
            user = (TextView) itemView.findViewById(R.id.page_user);
            tel = (TextView) itemView.findViewById(R.id.page_tel);
            location = (TextView) itemView.findViewById(R.id.page_location);
            yp = (DrawableTextView) itemView.findViewById(R.id.page_yp);
            lxl = (DrawableTextView) itemView.findViewById(R.id.page_lxl);
            page_jin = (DrawableTextView) itemView.findViewById(R.id.page_jin);
            page_today = (DrawableTextView) itemView.findViewById(R.id.page_today);
            page_rl = (RelativeLayout) itemView.findViewById(R.id.page_rl);



        }
    }

    public void addData(List<Tain_v2> data){
        if (mList != null) {
            mList.clear();
            mList.addAll(data);
            notifyDataSetChanged();
        }
    }

    public void addLastData(List<Tain_v2> data){
        mList.addAll(data);
        notifyDataSetChanged();
    }

    public interface OnItemClickListenter {
        void itemClickListenter(Tain_v2 dataBean, int position,boolean flag,int i);
    }

    public interface OnItemsClickListenter {
        void itemsClickListenter(Tain_v2 dataBean, int position);
    }
    public void setItemsClickListenter(OnItemsClickListenter itemClickListenter) {
        onItemsClickListenter = itemClickListenter;
    }


    public void setItemClickListenter(OnItemClickListenter itemClickListenter) {
        onItemClickListenter = itemClickListenter;
    }

    public void visible2gone(boolean flag){
        this.flag = flag;
    }

    public void visibleIsgone(boolean flag){
        this.isDay = flag;
    }

    public int getStay(){
        return stay;
    }

    public int getSuccess(){
        return success;
    }
}
